package com.css.fxfzypg.modules.message.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.message.repository.entity.MessageEntity;
import com.css.fxfzypg.modules.message.repository.entity.MessagePageList;
import com.css.fxfzypg.modules.message.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
    @Autowired
    MessageService messageService;

    @PostMapping("/addMessage")
    public RestResponse addMessage(@RequestBody MessageEntity messageEntity){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.addMessage(messageEntity);
            jsonObject.put("message", "新增成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "新增失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    @GetMapping("/queryMessage")
    public RestResponse queryMessage(String keyWords,String userId,int currentPage,int pageSize){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            MessagePageList pageList = messageService.queryMessage(keyWords,userId,currentPage,pageSize);
            jsonObject.put("message", "查询list成功");
            restResponse = RestResponse.succeed(pageList);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "查询list失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @SysLog("获取消息")
    @GetMapping("/getMessage")
    public List<MessageEntity> getMessage(String userId) {

        List<MessageEntity> page = messageService.getMessage(userId);
        return page;

    }

    @SysLog("获取用户有关消息")
    @GetMapping("/getMessageById")
    public RestResponse getMessageById(String messageId){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            MessageEntity messageEntity = messageService.getMessageById(messageId);
            jsonObject.put("message", "查询list成功");
            restResponse = RestResponse.succeed(messageEntity);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "获取失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看消息（变为已读）
     * @param params
     * @return
     */
    @PostMapping("/readMessage")
    public RestResponse readMessage(@RequestBody Map<String,String> params ){
        String messageId = params.get("messageId");
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.readMessage(messageId);
            jsonObject.put("message", "查看成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "查看失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 清空未读消息
     * @param userId
     * @return
     */
    @GetMapping("clearMessage")
    public RestResponse clearMessage(String userId){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.clearMessage(userId);
            jsonObject.put("message", "清除未读成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "清除未读失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据模块清除未读消息
     * @param messageModel
     * @return
     */
    @GetMapping("readMessageByModel")
    public RestResponse readMessageByModel(String messageModel){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean bool = messageService.readMessageByModel(messageModel);
            jsonObject.put("message", "清除未读成功");
            restResponse = RestResponse.succeed(bool);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "清除未读失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}